package br.com.guardaroupavirtual.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.guardaroupavirtual.helper.Diretorios;
import br.com.guardaroupavirtual.helper.LoginFacebook;
import br.com.guardaroupavirtual.model.Usuario;

@WebServlet("/LoginFBResponse")
public class LoginFBResponse extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private LoginFacebook loginFacebook;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		loginFacebook = new LoginFacebook();
		if (request.getParameter("code") != null && !request.getParameter("code").isEmpty()) {	
			Usuario usuario = loginFacebook.obterUsuarioFacebook(request.getParameter("code").toString());
			request.getSession().setAttribute("usuario", usuario);
			request.getRequestDispatcher(String.format(Diretorios.Home, "index.jsp")).forward(request, response);
		}
		if(request.getParameter("error_code") != null){
			request.getRequestDispatcher(String.format(Diretorios.Usuario, "login.jsp")).forward(request, response);			
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
